package com.neusoft.EYSystem.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.neusoft.EYSystem.dao.ClientDAO;
import com.neusoft.EYSystem.dao.UserOfClientDAO;
import com.neusoft.EYSystem.po.Client;
import com.neusoft.EYSystem.po.ResponseBean;
import com.neusoft.EYSystem.po.User;
import com.neusoft.EYSystem.po.UserOfClient;
import com.neusoft.EYSystem.redisDao.RedisDAO;
import com.neusoft.EYSystem.service.UserOfClientService;
import com.neusoft.EYSystem.utils.JWTTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/uoc")
public class UserOfClientController {

    @Autowired
    private UserOfClientDAO userOfClientDAO;

    @Autowired
    private ClientDAO clientDAO;

    @Autowired
    private UserOfClientService userOfClientService;

    @Autowired
    private RedisDAO redisDAO;

    @GetMapping("/getById")
    public ResponseBean<Map<String,Object>> getById(Long id) {
        ResponseBean<Map<String,Object>> response = new ResponseBean<>();
        try {
            UserOfClient userOfClient = userOfClientDAO.selectById(id);
            if (userOfClient != null) {
                response.setStatus(200);
                Client client = clientDAO.selectById(userOfClient.getClientId());
                response.setMsg("用户信息查询成功");
                response.setData(new HashMap<>() {{
                    put("userOfClient", userOfClient);
                    put("client", client);
                }});
            } else {
                response.setStatus(404);
                response.setMsg("用户信息不存在");
            }
        } catch (Exception e) {
            response.setStatus(500);
            response.setMsg("用户信息查询失败: " + e.getMessage());
        }
        return response;
    }

    @PostMapping("/update")
    public ResponseBean<Integer> update(UserOfClient userOfClient, Client client) {
        ResponseBean<Integer> response = new ResponseBean<>();
        try {
            int result = userOfClientDAO.updateById(userOfClient);
            int result1 = clientDAO.updateById(client);
            if (result > 0) {
                response.setStatus(200);
                response.setMsg("用户信息更新成功");
                response.setData(result);
            } else {
                response.setStatus(404);
                response.setMsg("用户信息不存在或未修改");
            }
        } catch (Exception e) {
            response.setStatus(500);
            response.setMsg("用户信息更新失败: " + e.getMessage());
        }
        return response;
    }

    @PostMapping("/login")
    public ResponseBean<Map<String, Object>> login(UserOfClient userOfClient) throws JsonProcessingException {
        ResponseBean<UserOfClient> rb = userOfClientService.getUser(userOfClient);
        ResponseBean<Map<String, Object>> tokenResponse;
        if(rb.getStatus() == 200) {
            UserOfClient user1 = rb.getData();
            user1.setPassword(null);
            ObjectMapper om = new ObjectMapper();
            String userJson = om.writeValueAsString(user1);
            Client client = clientDAO.selectById(user1.getClientId());
            // 生成JWT Token
            String token = JWTTool.createToken(userJson);
            redisDAO.set(user1.getClientId().toString(), token, 60 * 30, TimeUnit.SECONDS);
            tokenResponse = new ResponseBean<>(200, "登录成功", new HashMap<>() {{
                put("token", token);
                put("client", client);
                put("userOfClient", user1);
            }});
        } else {
            tokenResponse = new ResponseBean<>(400, rb.getMsg());
        }
        return tokenResponse;
    }

}
